const mongoose = require('../common/db');


const movieSchema = new mongoose.Schema({
    title: {
        type: String,
        required: true,
        unique: true,
        minlength: 1,
        maxlength: 255,
        trim: true
    },
    imgUrl: {
        type: String,
        required: true
    },
    description: {
        type: String,
        required: true,
        maxlength: 1024
    },
    year: {
        type: Number,
        required: true,
        min: 1900,
        max: 2099
    },
    director: {
        type: String,
        required: true,
        minlength: 1,
        maxlength: 255,
        trim: true
    },
    rating: {
        type: Number,
        required: true,
        min: 0,
        max: 10
    },
    genre: {
        type: String,
        minlength: 1,
        maxlength: 255,
        trim: true,
        required: true
    },
    supportCount: {
        type: Number,
        default: 0
    },
    downloadCount: {
        type: Number,
        default: 0
    },
    downloadUrl: {
        type: String,
        required: true,
        maxlength: 255,
        trim: true
    }
}, { timestamps: true });

movieSchema.statics.findByID = async function (id) {
    return await this.findOne({ _id: id }).exec();
};
movieSchema.statics.query = async function (condition) {
    return await this.find(condition).exec();
};

movieSchema.statics.findAll = async function () {
    return await this.find().sort({ rating: -1 }).exec();
};




const Movie = mongoose.model('Movie', movieSchema);

module.exports = Movie;
